package com.shopping.controller;

import com.shopping.pojo.Category;
import com.shopping.pojo.User;
import com.shopping.pojo.vo.*;
import com.shopping.service.*;
import com.shopping.utils.R;
import com.shopping.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "管理员控制类")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private ProductService productService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CommentService commentService;

    @ApiOperation("管理员登录接口")
    @PostMapping("api/admin/login")
    public R login(String userName, String password) {
        // 判断用户名密码是否正确
        if (adminService.checkUserNameAndPassword(userName, password)) {
            // 生成token
            String token = TokenUtils.token(userName, "admin", 1);
            LoginInfo userByToken = TokenUtils.getUserByToken(token);
            return R.ok().message("登录成功").data("admin", userByToken).data("token", token);
        } else {
            return R.error().message("用户名或密码错误");
        }
    }

    @ApiOperation("获取用户列表接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectUserByQuery")
    public R selectUserByQuery(int page, int limit, String userName) {
        int start = (page - 1) * limit;
        List<User> userList = userService.selectUserByQuery(userName, start, limit);
        int userListNum = userService.getUserNum(userName);
        return R.ok().message("获取用户信息列表成功~").data("userList", userList).data("total", userListNum);
    }

    @ApiOperation("用户加入黑名单接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateJoinBlackList")
    public R updateJoinBlackList(Integer userId) {
        if (userService.updateJoinBlackList(userId)) {
            return R.ok().message("加入用户黑名单成功");
        } else {
            return R.error().message("加入用户黑名单失败");
        }
    }

    @ApiOperation("用户移出黑名单接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateRemoveBlackList")
    public R updateRemoveBlackList(Integer userId) {
        if (userService.updateRemoveBlackList(userId)) {
            return R.ok().message("移出用户黑名单成功");
        } else {
            return R.error().message("移出用户黑名单失败");
        }
    }

    @ApiOperation("获取商家列表接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectBusinessByQuery")
    public R selectBusinessByQuery(int page, int limit, String businessName, String userName) {
        int start = (page - 1) * limit;
        List<BusinessInfo> businessList = businessService.selectBusinessByQuery(businessName, userName, start, limit);
        int businessListNum = businessService.getBusinessNum(businessName, userName);
        return R.ok().message("获取商家信息列表成功~").data("businessList", businessList).data("total", businessListNum);
    }

    @ApiOperation("商家注册审核接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateBusinessStatusSuccess")
    public R updateBusinessStatusSuccess(Integer id) {
        if (businessService.updateBusinessStatusSuccess(id)) {
            return R.ok().message("审核通过成功");
        } else {
            return R.error().message("审核通过失败");
        }
    }

    @ApiOperation("商家注册删除接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/deleteBusiness")
    public R deleteBusiness(Integer id) {
        if (businessService.deleteBusiness(id)) {
            return R.ok().message("移出用户黑名单成功");
        } else {
            return R.error().message("移出用户黑名单失败");
        }
    }

    @ApiOperation("商家加入黑名单接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateBusinessJoinBlackList")
    public R updateBusinessJoinBlackList(Integer id) {
        if (businessService.updateBusinessJoinBlackList(id)) {
            return R.ok().message("加入商家黑名单成功");
        } else {
            return R.error().message("加入商家黑名单失败");
        }
    }

    @ApiOperation("商家移出黑名单接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateBusinessRemoveBlackList")
    public R updateBusinessRemoveBlackList(Integer id) {
        if (businessService.updateBusinessRemoveBlackList(id)) {
            return R.ok().message("移出商家黑名单成功");
        } else {
            return R.error().message("移出商家黑名单失败");
        }
    }

    @ApiOperation("获取商品列表接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectProductByQuery")
    public R selectProductByQuery(int page, int limit, String productName, String businessName, String cateName) {
        int start = (page - 1) * limit;
        List<ProductSpecInfo> productList = productService.selectProductByQuery(productName, businessName, cateName, start, limit);
        int productListNum = productService.getProductNum(productName, businessName, cateName);
        return R.ok().message("获取商品信息列表成功~").data("productList", productList).data("total", productListNum);
    }

    @ApiOperation("强制下架商品接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateProductStatusCompulsory")
    public R updateProductStatusCompulsory(Integer productId) {
        if (productService.updateProductStatusCompulsory(productId)) {
            return R.ok().message("强制下架商品成功");
        } else {
            return R.error().message("强制下架商品失败");
        }
    }

    @ApiOperation("允许商品上架接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateProductStatusAllow")
    public R updateProductStatusAllow(Integer productId) {
        if (productService.updateProductStatusAllow(productId)) {
            return R.ok().message("允许商品上架成功");
        } else {
            return R.error().message("允许商品上架失败");
        }
    }

    @ApiOperation("获取商品分类列表接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectCategoryByQuery")
    public R selectCategoryByQuery(String cateName, int page, int limit) {
        int start = (page - 1) * limit;
        List<Category> categoryList = categoryService.selectCategoryByQuery(cateName, start, limit);
        int CategoryListNum = categoryService.getCategoryNum(cateName);
        return R.ok().message("获取商品分类列表成功~").data("categoryList", categoryList).data("total", CategoryListNum);
    }

    @ApiOperation("获取商品分类接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectCategory")
    public R selectCategory() {
        List<String> categoryList = categoryService.selectCategory();
        return R.ok().message("获取商品分类列表成功~").data("categoryList", categoryList);
    }

    @ApiOperation("获取类别分类接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectClassify")
    public R selectClassify() {
        List<String> classifyList = categoryService.selectClassify();
        return R.ok().message("获取商品类别分类列表成功~").data("classifyList", classifyList);
    }

    @ApiOperation("新增商品分类接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/insertCate")
    public R insertCate(Category category) {
        if (categoryService.insertCate(category)) {
            return R.ok().message("新增商品分类成功");
        } else {
            return R.error().message("新增商品分类失败");
        }
    }

    @ApiOperation("修改商品分类接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/updateCate")
    public R updateCate(Category category) {
        if (categoryService.updateCate(category)) {
            return R.ok().message("修改商品分类成功");
        } else {
            return R.error().message("修改商品分类失败");
        }
    }

    @ApiOperation("删除商品分类接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/deleteCate")
    public R deleteCate(Integer categoryId) {
        if (productService.checkProductByCateId(categoryId)) {
            return R.error().message("该分类下仍有商品，删除商品分类失败");
        } else {
            if (categoryService.deleteCate(categoryId)) {
                return R.ok().message("删除商品分类成功");
            } else {
                return R.error().message("删除商品分类失败");
            }
        }
    }

    @ApiOperation("获取订单列表接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectOrdersByQuery")
    public R selectOrdersByQuery(int page, int limit, String orderId, String userName) {
        int start = (page - 1) * limit;
        List<OrdersInfo> ordersList = ordersService.selectOrdersByQuery(orderId, userName, start, limit);
        int ordersListNum = ordersService.getOrdersNum(orderId, userName);
        return R.ok().message("获取订单信息列表成功~").data("ordersList", ordersList).data("total", ordersListNum);
    }

    @ApiOperation("获取评论列表接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/selectCommentByQuery")
    public R selectCommentByQuery(int page, int limit, String userName, String productName) {
        int start = (page - 1) * limit;
        List<CommentInfo> commentList = commentService.selectCommentByQuery(userName, productName, start, limit);
        int commentListNum = commentService.getCommentNum(userName, productName);
        return R.ok().message("获取评论列表成功~").data("commentList", commentList).data("total", commentListNum);
    }

    @ApiOperation("删除评论接口")
    @ApiImplicitParam(paramType = "header", name = "token", required = true)
    @PostMapping("api/admin/deleteComment")
    public R deleteComment(Integer commentId) {
        if (commentService.deleteComment(commentId)) {
            return R.ok().message("删除评论成功");
        } else {
            return R.error().message("删除评论失败");
        }
    }
}
